<?php

namespace app\common\model;

use think\Db;
use think\Exception;
use think\Model;

class House extends Model
{

    const TYPE=[
//        1单间，2 1室1厅，2两室1厅,套房，4其他'
        1=>'单人间',
        2=>'1室1厅',
        3=>'2室1厅',
        5=>'其他',
    ];

    const STATUS=[
        0=>'已发布',
        2=>'已出租',
        -1=>'下架',
        ];

    const TAB=[
        1=>'四面采光',
        2=>'通风好',
        3=>'交通便利',
        4=>'环境优雅',
        5=>'设备齐全',
    ];

   public function getList($params)
   {
       $ads=model("Ads")
           ->where(['is_show'=>1])
           ->order("sort_id  desc,id desc")
           ->page(1,5)
           ->select()->each(function ($item){
               $item['image_path']=model("File")->getPath($item['image_id']);
           });
       $where=[];
       if ($params['province_id']){
           $where[]=['province_id','=',$params['province_id']];
       }
       if ($params['city_id']){
           $where[]=['city_id','=',$params['city_id']];
       }
       if ($params['area_id']){
           $where[]=['area_id','=',$params['area_id']];
       }
       if ($params['keywords']){
           $where[]=['title','like','%'.$params['keywords'].'%'];
       }
       if ($params['zujin']==1){//租金500以内
           $where[]=['price','<',500];
       }
       if ($params['zujin']==1){//租金500以内
           $where[]=['price','<',500];
       }
       if ($params['zujin']==2){//租金500-1000以内
           $where[]=['price','>',500];
           $where[]=['price','<',1000];
       }
       if ($params['zujin']==3){//租金1000-2000以内
           $where[]=['price','>',1000];
           $where[]=['price','<',2000];
       }
       if ($params['zujin']==4){//租金2000以上
           $where[]=['price','>',2000];
       }
       if ($params['tab']){
            $where[]=['FIND_IN_SET(:uid,uid)',['uid'=>$params['tab']]];
       }
       if ($params['area_big']==1){//10平以内
           $where[]=['area_big','<',10];
       }
       if ($params['area_big']==2){//10-30平以内
           $where[]=['area_big','>',10];
           $where[]=['area_big','<',30];
       }
       if ($params['type']){//房间类型
           $where[]=['type','=',$params['type']];
       }
       if ($params['area_big']==3){//30平以内
           $where[]=['area_big','>',30];
       }

       $where[]=['status','not in',[-1,2]];
           $house=model("House")
               ->where($where)
               ->order("id desc")
               ->page($params['page'],8)
               ->field("images,area_big,id,tab,type,price,title")
               ->select();
        $shuxing=model("House")::TYPE;
        if (!empty($house)){
            foreach ($house as $key=>$val){
                $house[$key]['image_path']=model("File")->getPath($val['images']);
                $spec=isset($shuxing[$val['type']])?$shuxing[$val['type']]:'';
                $specs=$val['area_big'].'㎡|'.$spec;
                $house[$key]['spec_title']=$specs;
                $house[$key]['sign']=model("Sign")->get_house_spec($val['tab']);

            }
        }
        $province=model("Area")->where(['level'=>1])->field("id,title")->select();
       $data=[
           'ads'=>$ads,
           'house'=>$house,
           'province'=>$province,
           'type'=>model("House")::TYPE,
           'zujin'=>[0=>'500以内',1=>'500~1000',2=>'1000~2000',3=>'2000以上'],
           'area_big'=>[0=>'10平米以内',2=>'10~30平',3=>'大于30平'],
       ];

       return $data;

   }




}
